﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Wintellect.LiveFx.Cloudboard
{
	/// <summary>
	/// Interaction logic for HyperLink.xaml
	/// </summary>
  public partial class Hyperlink : ContentControl
	{
    public event EventHandler Click;
    public Hyperlink()
		{
			this.InitializeComponent();
		}
    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
      this.CaptureMouse();
      base.OnMouseLeftButtonDown(e);
    }
    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
    {
      if (this.IsMouseCaptured)
      {
        this.ReleaseMouseCapture();
        this.OnClick();
      }
      base.OnMouseLeftButtonUp(e);
    }
    private void OnClick()
    {
      if (this.Click != null)
      {
        this.Click(this, new EventArgs());
      }
    }
	}

  public class HyperlinkDataTemplateSelector : DataTemplateSelector
  {
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
      DataTemplate output = null;
      ContentPresenter presenter = container as ContentPresenter;
      if (presenter != null)
      {
        Hyperlink hyperlink = presenter.TemplatedParent as Hyperlink;

        if (hyperlink != null)
        {
          string contentAsString = item as string;
          if (contentAsString != null)
          {
            output = hyperlink.FindResource("textTemplate") as DataTemplate;
          }
        }
      }
      
      if (output == null)
      {
        output = base.SelectTemplate(item, container);
      }

      return output;
    }
  }
}